package com.qjc.demo.algorithm.环形链表;

/**
 * @ClassName: RingLink
 * @Description: 环形链表
 * @Author: qjc
 * @Date: 2022/5/6 11:12 AM
 */
public class RingLink {

    public static boolean isRing(RingNode head) {
        if (head == null) return false;
        RingNode slow = head, fast = head.next;
        while (fast != null && fast.next != null) {
            if (slow == fast) {
                return true;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return false;
    }

    public static void main(String[] args) {
        RingNode n1 = new RingNode(1);
        RingNode n2 = new RingNode(2);
        RingNode n3 = new RingNode(3);

        n1.next = n2;
        n2.next = n3;
        n3.next = null;
        System.out.println(isRing(n1));

    }

}

class RingNode {
    int id;
    RingNode next;

    public RingNode(int id) {
        this.id = id;
    }
}
